package com.yanboo.generator.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.logging.annotation.Log;
import com.yanboo.generator.domain.DatabaseTypeMap;
import com.yanboo.generator.service.DatabaseTypeMapService;
import com.yanboo.generator.service.dto.DatabaseTypeMapQueryCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import com.yanboo.common.utils.PageResult;
import com.yanboo.generator.service.dto.DatabaseTypeMapDto;

/**
 * @author fdq
 * @date 2023-12-01
 **/
@CustomController
@RequiredArgsConstructor
@Tag(name = "数据库类型对照管理")
@RequestMapping("/api/databaseTypeMap")
public class DatabaseTypeMapController {

    private final DatabaseTypeMapService databaseTypeMapService;

    @Log("导出数据")
    @Operation(summary = "导出数据")
    @GetMapping(value = "/download")
    public void exportDatabaseTypeMap(HttpServletResponse response, DatabaseTypeMapQueryCriteria criteria) throws IOException {
        databaseTypeMapService.download(databaseTypeMapService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询数据库类型对照")
    @Operation(summary = "查询数据库类型对照")
    public ResponseEntity<PageResult<DatabaseTypeMapDto>> queryDatabaseTypeMap(DatabaseTypeMapQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(databaseTypeMapService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增数据库类型对照")
    @Operation(summary = "新增数据库类型对照")
    public ResponseEntity<Object> createDatabaseTypeMap(@Validated @RequestBody DatabaseTypeMap resources){
        databaseTypeMapService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改数据库类型对照")
    @Operation(summary = "修改数据库类型对照")
    public ResponseEntity<Object> updateDatabaseTypeMap(@Validated @RequestBody DatabaseTypeMap resources){
        databaseTypeMapService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除数据库类型对照")
    @Operation(summary = "删除数据库类型对照")
    public ResponseEntity<Object> deleteDatabaseTypeMap(@RequestBody Long[] ids) {
        databaseTypeMapService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
